package controllers

import (
	"e-shops/models"
	beego "github.com/beego/beego/v2/server/web"
)

type ShopController struct {
	beego.Controller
}

func (c *ShopController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	s := models.Shop{
		Address: c.GetString("address"),
		Manager: c.GetString("manager"),
		Name:    c.GetString("name"),
	}
	err := models.DB.Create(&s).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器：zzz...",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "我来到，我看见，我记录",
	}
	c.ServeJSON()
}
